﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ChartEdit
{
    public enum KeyType
    {
        Note,
        GEvent,
        TEvent,
        MoveUp,
        MoveDown,
        QuantizeUp,
        QuantizeDown,
        Delete,
        Sync,
        Anchor,
        Selection,
        Play,
        SpeedUp,
        SlowDown
    }
    public class KMKey
    {
        [XmlAttribute]
        public KeyType Type;
        public NoteType NType;
        public int Key;
        public int Value;
        public KMKey()
        {
        }
        public KMKey(KeyType type, NoteType nType, int key, int value)
        {
            Type = type;
            NType = nType;
            Key = key;
            Value = value;
        }
    }
    public class KeyManager
    {
        public List<int> KbKeys = new List<int>();
        public List<int> KbKeysOrdered = new List<int>();
        public List<KMKey> Keys = new List<KMKey>();
        public void Press(int key)
        {
            if (KbKeys.Count()==0 || KbKeys.Last() != key)
                KbKeysOrdered.Add(key);
            foreach (int i in KbKeys)
            {
                if (i == key)
                    return;
            }
            KbKeys.Add(key);
        }
        public void Release(int key)
        {
            KbKeys.Remove(key);
            if(KbKeys.Count()==0)
                KbKeysOrdered.Clear();
        }
        public List<KMKey> KeysPressed() { return KeysPressed(false); }
        public List<KMKey> KeysPressed(bool ordered)
        {
            List<int> keyList;
            if (ordered)
                keyList = KbKeysOrdered;
            else
                keyList = KbKeys;

            List<KMKey> pressedKeys = new List<KMKey>();
            foreach (int k in keyList)
            {
                foreach (KMKey key in Keys)
                {
                    if (key.Key == k)
                    {
                        pressedKeys.Add(key);
                    }
                }
            }
            return pressedKeys;
        }
        public KeyManager()
        {
            Keys.Add(new KMKey(KeyType.Note,NoteType.Regular,(int)System.Windows.Forms.Keys.D1,0));
            Keys.Add(new KMKey(KeyType.Note,NoteType.Regular,(int)System.Windows.Forms.Keys.D2,1));
            Keys.Add(new KMKey(KeyType.Note,NoteType.Regular,(int)System.Windows.Forms.Keys.D3,2));
            Keys.Add(new KMKey(KeyType.Note,NoteType.Regular,(int)System.Windows.Forms.Keys.D4,3));
            Keys.Add(new KMKey(KeyType.Note,NoteType.Regular,(int)System.Windows.Forms.Keys.D5,4));
            Keys.Add(new KMKey(KeyType.Note, NoteType.Regular, (int)System.Windows.Forms.Keys.D6, 5));
            Keys.Add(new KMKey(KeyType.MoveUp, NoteType.Regular, (int)System.Windows.Forms.Keys.Up, 0));
            Keys.Add(new KMKey(KeyType.MoveDown, NoteType.Regular, (int)System.Windows.Forms.Keys.Down, 0));
            Keys.Add(new KMKey(KeyType.QuantizeDown, NoteType.Regular, (int)System.Windows.Forms.Keys.Left, 0));
            Keys.Add(new KMKey(KeyType.QuantizeUp, NoteType.Regular, (int)System.Windows.Forms.Keys.Right, 0));
            Keys.Add(new KMKey(KeyType.Play, NoteType.Regular, (int)System.Windows.Forms.Keys.Space, 0));
        }
    }
}
